package com.igeek.boot.exception;

import com.igeek.boot.result.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * @Author fengqq
 * @Description @ControllerAdvice+@ExceptionHandler全局异常处理器
 * @Date 2024/8/19  14:12
 *
 * 方式二：@ControllerAdvice
 *      1.@ControllerAdvice
 *      2.@RestControllerAdvice 等价于 @ControllerAdvice+@ResponseBody
 *
 *      3.底层是ExceptionHandlerExceptionResolver 支持的
 *
 *      4.使用场景：更适合于 前后端分离
 *
 */
//@ControllerAdvice
//@RestControllerAdvice
@Slf4j
public class MyGlobalExceptionHandler {
    //前后不分离 @ControllerAdvice
    /*@ExceptionHandler(ArithmeticException.class)
    public String error(Exception ex){
        log.info("异常信息：{}",ex.getMessage());
        return "redirect:/login.html";
    }*/


    //前后分离 针对自定义异常  @RestControllerAdvice
    @ExceptionHandler(CustomException.class)
    public Result errorByCustom(CustomException ex){
        return new Result(500,ex.getMessage());
    }
    //前后分离 针对非自定义异常  @RestControllerAdvice
    @ExceptionHandler(Exception.class)
    public Result errorByException(Exception ex){
        return new Result(500,"系统崩溃");
    }


}
